﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    public static class ArrayExtensions
    {
        /// <summary>
        /// 打乱顺序
        /// </summary>
        public static void Shuffle<T>(this T[] array)
        {
            for(int i = 0; i < array.Length; i++)
            {
                int index = UnityEngine.Random.Range(0, array.Length);
                T t = array[index];
                array[index] = array[i];
                array[i] = t;
            }
        }

        public  static T[] RemoveDuplicate<T>(this T[] array)
        {
            HashSet<T> hash = new HashSet<T>();
            foreach(var t in array)
            {
                if (!hash.Contains(t)) hash.Add(t);
            }

            T[] ts = new T[hash.Count];
            int index = 0;
            foreach (var t in hash) ts[index++] = t;
            return ts;

        }

        public static U[] Cast<T,U>(this T[] array) where T: class where U : class, T
        {
            return Array.ConvertAll<T, U>(array, (t) =>  t as U);
        }

    }
}

